package com.mydemo.zookeeper;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

import java.io.IOException;
import java.util.List;

public class ZookeeperUtils {

    public static ZooKeeper zk;

    static {
        try {
            zk = new ZooKeeper("127.0.0.1:2181", 5000,
                        new Watcher(){
                            @Override
                            public void process(WatchedEvent watchedEvent) {

                            }
                        });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception {

        listNodes("/");
    }

    public static void listNodes(String path) throws Exception {

        List<String> list = zk.getChildren(path,false);
        for (String string:list){
            String s = "";
            String subPath = "";
            if (path.endsWith("/")){
                s = path + string;
                subPath = path + string;
            } else {
                s = path + "/"+ string;
                subPath = path +"/"+ string;
            }
            System.out.println(s);

            listNodes(subPath);
        }
    }
}
